package com.kgc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kgc.dto.EvaluateDetailDto;
import com.kgc.dto.EvaluateDto;
import com.kgc.dto.ModelDto;
import com.kgc.mapper.EvaluateMapper;
import com.kgc.mapper.MalfunctionMapper;
import com.kgc.mapper.MalfunctionOptionsMapper;
import com.kgc.pojo.*;
import com.kgc.service.EvaluateDetailService;
import com.kgc.service.EvaluateService;
import com.kgc.service.ModelService;
import com.kgc.utils.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 评估信息 服务实现类
 * </p>
 *
 * @author zsy
 * @since 2021-08-04
 */
@Service
public class EvaluateServiceImpl extends ServiceImpl<EvaluateMapper, Evaluate> implements EvaluateService {

    @Autowired
    private MalfunctionOptionsMapper optionsMapper;
    @Autowired
    private MalfunctionMapper malfunctionMapper;
    @Autowired
    private EvaluateDetailService evaluateDetailService;
    @Autowired(required = false)
    private EvaluateMapper evaluateMapper;
    @Autowired
    private ModelService modelService;

    @Override
    public Evaluate assess(Long modelId, String optionIds, Long id) {
        List<MalfunctionOptions> optionList = new ArrayList<>();

        // 总金额，默认是0
        BigDecimal totalPrice = new BigDecimal(0);
        Evaluate evaluate = null;
        //将optionIds变成一个集合
        Set<Long> opIds = StringUtil.string2LongSet(optionIds, ",");
        for (Long opId : opIds) {
            MalfunctionOptions option = optionsMapper.selectById(opId);
            //optionList.add(option);
            //获取故障id
            Long malfId = option.getMalfId();

            Malfunction malfunction = malfunctionMapper.selectById(malfId);
            // 以后在工作中，涉及到钱的都需要使用BigDecimal
            BigDecimal topPrice = malfunction.getMaintainTopPrice();
            // ratio 百分比
            Integer ratio = option.getRatio();
            // 单个故障详情的维修价格
            // topPrice.multiply(new BigDecimal(ratio * 0.01)); 400(topPrice) *(multiply) 1(new BigDecimal(ratio * 0.01))
            BigDecimal onePrice = topPrice.multiply(new BigDecimal(ratio * 0.01));
            // 将单个价格添加到总价格内
            totalPrice = totalPrice.add(onePrice);
        }
        // 订单的金额
        BigDecimal subscription = totalPrice.multiply(new BigDecimal(0.1));
        evaluate = new Evaluate();
        evaluate.setModelId(modelId);
        evaluate.setPrice(totalPrice);
        evaluate.setSubscription(subscription);
        evaluate.setCreatedUserId(id);
        // 保存evaluate对象
        this.save(evaluate);
        // 保存评估详细信息
        evaluateDetailService.saveBatchByOptions(evaluate.getId(), optionList);
        return evaluate;
    }

    @Override
    public EvaluateDto assessInfo(Long id) {
        EvaluateDto evaluateDto = new EvaluateDto();
        //获取评估对象
        Evaluate evaluate = this.getById(id);
        BeanUtils.copyProperties(evaluate, evaluateDto);
        //获取商品对象
        Model model = modelService.getById(evaluateDto.getModelId());
        ModelDto modelDto = new ModelDto();
        BeanUtils.copyProperties(model, modelDto);
        //拼接图片
        modelService.formatModel(modelDto);
        //获取评估详情
        List<EvaluateDetailDto> detailDtoList = new ArrayList<>();
        QueryWrapper<EvaluateDetail> wrapper = new QueryWrapper<>();
        wrapper.eq("evaluateId", id);
        //转换为dto
        List<EvaluateDetail> detailList = evaluateDetailService.list(wrapper);
        for (EvaluateDetail evaluateDetail : detailList) {
            EvaluateDetailDto evaluateDetailDto = new EvaluateDetailDto();
            BeanUtils.copyProperties(evaluateDetail, evaluateDetailDto);
            detailDtoList.add(evaluateDetailDto);
        }
        evaluateDto.setModel(modelDto);
        evaluateDto.setDetailList(detailDtoList);
        return evaluateDto;
    }
}
